//
//  WarViewController.m
//  HappyNews
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "WarViewController.h"

#import "WarHeaderCollectionReusableView.h"

#import "WarCollectionViewCell.h"

#import "DCPicScrollView.h"

#import "DCWebImageManager.h"

#import "HNRequestManager.h"

#import "RecreationViewModel.h"

#define WarURL @"http://vcsp.ifeng.com/vcsp/appData/recommendGroupByTeamid.do?channelId=100377-0&showType=double&useType=iPhone&positionId=&adapterNo=6.11.1&isNotModified=0"


@interface WarViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *WarCollection;

@property (nonatomic, strong) NSMutableArray *headerWarArr;

@property (nonatomic, strong) NSMutableArray *titleWarArr;

@property (nonatomic, strong) NSMutableArray *imageWarArr;

@property (nonatomic, strong) NSMutableArray *WarCellArr;

@property (nonatomic, strong) WarHeaderCollectionReusableView *header;

@end

@implementation WarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addWarConnection];
    
    [self setWarUpData];
    
    _titleWarArr = [NSMutableArray array];
    
    _imageWarArr = [NSMutableArray array];
    
    _headerWarArr = [NSMutableArray array];
    
    _WarCellArr = [NSMutableArray array];
    
}
- (void)setWarUpData
{
    [HNRequestManager requestWithUrlString:WarURL parDic:nil method:GET finish:^(NSData *data) {
        
        NSDictionary *WarDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *warArray = WarDic[@"header"];
        
        for (NSMutableDictionary *headerWarDic in warArray) {
            
            RecreationViewModel *model = [[RecreationViewModel alloc]init];
            
            [model setValuesForKeysWithDictionary:headerWarDic];
            
            
            [_titleWarArr addObject:model.title];
            
            [_imageWarArr addObject:model.image];
            
            [_headerWarArr addObject:model];
        }
        
        
        
        
        // cell
        NSArray *bodyListArr = WarDic[@"bodyList"];
        
        for (NSMutableDictionary *WarOneDic in bodyListArr) {
            
            NSArray *videoListArr = WarOneDic[@"videoList"];
            
            for (NSMutableDictionary *WarTwoDic in videoListArr) {
                RecreationViewModel *model = [[RecreationViewModel alloc]init];
                
                [model setValuesForKeysWithDictionary:WarTwoDic];
                
                [_WarCellArr addObject:model];
            }
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [_WarCollection reloadData];
        });
        
    } error:^(NSError *error) {
        
    }];
    
    
}

- (void)addWarConnection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.minimumInteritemSpacing = 10;
    
    layout.minimumLineSpacing = 10;
    
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake((kScreenWidth - 30) / 2, kScreenHeight / 3.7);
    
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, kScreenHeight / 3);
    
    _WarCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 155) collectionViewLayout:layout];
    
    _WarCollection.delegate = self;
    
    _WarCollection.dataSource = self;
    
    _WarCollection.backgroundColor = [UIColor whiteColor];
    
    [_WarCollection registerClass:[WarCollectionViewCell class] forCellWithReuseIdentifier:@"WarCollectionViewCell"];
    
    [_WarCollection registerClass:[WarHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WarHeaderCollectionReusableView"];
    
    [self.view addSubview:_WarCollection];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _WarCellArr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    _header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"WarHeaderCollectionReusableView" forIndexPath:indexPath];
    _header.titleWarArr = _titleWarArr;
    _header.imageWarArr = _imageWarArr;
    _header.headerWarArr = _headerWarArr;
    
    
    
    __weak NSArray *myheaderWarArr = _headerWarArr;
    
    __weak id myDelegate = _delegate;
    
    [_header.picView setImageViewDidTapAtIndex:^(NSInteger index) {
        
        
        if (myDelegate && [myDelegate respondsToSelector:@selector(addWarNav:)]) {
            [myDelegate addWarNav:myheaderWarArr[index]];
        }
        
    }];
    
    
    return _header;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WarCollectionViewCell" forIndexPath:indexPath];
    
    cell.model = _WarCellArr[indexPath.row];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_delegate && [_delegate respondsToSelector:@selector(addWarNav:)]) {
        [_delegate addWarNav:_WarCellArr[indexPath.row]];
    }
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
